﻿using AbpPcClient.WpfClient.Service.Materials;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbpPcClient.WpfClient.ViewModels
{
    public class WuZiListViewModel: BindableBase
    {
        /// <param name="tenantService">货主合同</param>
        public WuZiListViewModel(MaterialsService ownerService, IRegionManager regionManager)
        {
            this.ownerService = ownerService;
            this.regionManager = regionManager;
            //打开详情页的方法绑定
            OpenCommand = new DelegateCommand<OwnerDto>(OpenUpdate);
            Init();
        }
        private PagedOwnerRequestDto _pagedResultRequestDto;
        /// <summary>
        ///查询dto 
        /// </summary>
        public PagedOwnerRequestDto PagedResultRequestDto
        {
            get { return _pagedResultRequestDto; }
            set
            {
                _pagedResultRequestDto = value; RaisePropertyChanged();
            }
        }
        private readonly OwnerService ownerService;
        private readonly IRegionManager regionManager;
        private ObservableCollection<OwnerDto> _ownerDtos;
        /// <summary>
        /// 货主合同列表
        /// </summary>
        public ObservableCollection<OwnerDto> OwnerDtos
        {
            get
            {
                return _ownerDtos;
            }
            set
            {
                _ownerDtos = value; RaisePropertyChanged();
            }
        }
        /// <summary>
        /// 窗体初始化方法
        /// </summary>
        public void Init()
        {
            _pagedResultRequestDto = new PagedOwnerRequestDto()
            {
                SkipCount = 0,
                MaxResultCount = 100
            };
            _ownerDtos = new ObservableCollection<OwnerDto>();
            SearchPage();
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        public void SearchPage()
        {
            var reslut = ownerService.GetAll(_pagedResultRequestDto);
            if (_ownerDtos != null) _ownerDtos.Clear();
            if (reslut.Success)
            {
                ///序列化返回结果
                _ownerDtos = JsonConvert.DeserializeObject<ObservableCollection<OwnerDto>>(reslut.Result.items.ToString());
            }
        }
        public DelegateCommand<OwnerDto> OpenCommand { get; private set; }
        /// <summary>
        /// 打开详情页的方法
        /// </summary>
        /// <param name="tenantDto"></param>
        public void OpenUpdate(OwnerDto ownerDto)
        {
            var para = new NavigationParameters();
            para.Add("td", ownerDto);
            regionManager.Regions[PrismManager.MainViewRegionName].RequestNavigate("TenantManagerDefaultView", para);
        }
    }
}
